//
//  MakefitrndsReleaseViewController.m
//  cityStar
//
//  Created by joyban on 15-1-26.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "MakefitrndsReleaseViewController.h"

#import "MakefirendsReleaseOneCell.h"
#import "MakefirendsReleaseTwoCell.h"
#import "SetUpImageCell.h"
#import "UIColor+FlatUI.h"
#import "RIButtonItem.h"
#import "UIActionSheet+Blocks.h"
#import "UpLoadImageManager.h"
#import "MakefriendsViewController.h"

NSString *const MakefirendsReleaseOneCellID = @"MakefirendsReleaseOneCellID";
NSString *const MakefirendsReleaseTwoCellID = @"MakefirendsReleaseTwoCellID";
NSString *const SetUpImageCellID = @"SetUpImageCellID";


@interface MakefitrndsReleaseViewController ()<setUpImageCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *ReleaseTableView;
@property (strong, nonatomic) MakefirendsReleaseOneCell *OnedetailCell;
@property (strong, nonatomic) MakefirendsReleaseTwoCell *TwodetailCell;
@property (strong, nonatomic) SetUpImageCell *setUpImageCell;
@property (nonatomic,assign) int buttonTag;
@property (nonatomic,strong) NSMutableDictionary *dictPhotoIDs;

@end

@implementation MakefitrndsReleaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"婚介交友";
    [self addBackButton];
    [self setUpTableViews];
    self.dictPhotoIDs = [[NSMutableDictionary alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.ReleaseTableView registerNib:[MakefirendsReleaseOneCell nib] forCellReuseIdentifier:MakefirendsReleaseOneCellID];
    [self.ReleaseTableView registerNib:[MakefirendsReleaseTwoCell nib] forCellReuseIdentifier:MakefirendsReleaseTwoCellID];
    [self.ReleaseTableView registerNib:[SetUpImageCell nib] forCellReuseIdentifier:SetUpImageCellID];
}

#pragma mark - SetUpImageDelegate

-(void)upLoadImageWithButtonTag:(int)sender
{
    [self.view endEditing:YES];
    self.buttonTag = sender;
    __block UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    RIButtonItem *takePhotoButton = [RIButtonItem itemWithLabel:@"拍照" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    RIButtonItem *choosePhotoButton = [RIButtonItem itemWithLabel:@"从相册选取" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    
    UIActionSheet *imageSourcePickActionSheet;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:takePhotoButton,choosePhotoButton,nil];
    }else{
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:choosePhotoButton,nil];
    }
    [imageSourcePickActionSheet showInView:self.view];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:^{
        switch (self.buttonTag) {
            case 1:
            {
                [self.setUpImageCell.btnAddImage01 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage01 setTitle:@"" forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage01 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[0]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                
                break;
            case 2:
            {
                [self.setUpImageCell.btnAddImage02 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage02 setTitle:@"" forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage02 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[1]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 3:
            {
                [self.setUpImageCell.btnAddImage03 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage03 setTitle:@"" forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage03 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[2]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 4:
            {
                [self.setUpImageCell.btnAddImage04 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage04 setTitle:@"" forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage04 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[3]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 5:
            {
                [self.setUpImageCell.btnAddImage05 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage05 setTitle:@"" forState:UIControlStateNormal];
                [self.setUpImageCell.btnAddImage05 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[4]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
                
            default:
                break;
        }
    }];
}


-(void)setUpImageCellPublishButton
{
    NSString *errMsg;
    if (self.OnedetailCell.titleText.text.length == 0) {
        errMsg = @"请输入标题";
    }else if (self.OnedetailCell.ageText.text.length == 0){
        errMsg = @"请输入年龄";
    }else if (self.OnedetailCell.MarriageText.text.length == 0){
        errMsg = @"请选择婚姻状况";
    }else if (self.OnedetailCell.addressText.text.length == 0){
        errMsg = @"请输入户籍";
    }else if (self.OnedetailCell.NowAddressText.text.length == 0){
        errMsg =@"请选择所在区域";
    }else if (self.OnedetailCell.ContactTetx.text.length == 0){
        errMsg = @"请输入联系人";
    }else if (self.OnedetailCell.ContactTelphoneText.text.length == 0){
        errMsg = @"请输入联系方式";
    }else if (self.TwodetailCell.EducationText.text.length == 0){
        errMsg = @"请选择学历";
    }else if (self.TwodetailCell.WorkTypeText.text.length == 0){
        errMsg = @"请输入工作性质";
    }else if (self.TwodetailCell.MonthlySalaryText.text.length == 0){
        errMsg = @"请选择月薪";
    }else if (self.TwodetailCell.carText.text.length == 0){
        errMsg = @"请选择房车";
    }else if (self.TwodetailCell.RequirementText.text.length == 0){
        errMsg = @"请输入择偶要求";
    }else if (self.setUpImageCell.fieldIntroduction.text.length == 0){
        errMsg = @"请输入自我介绍";
    }
    if (errMsg) {
        BossSystemShowAlertViewWithTitle(errMsg)
        return;
    }
    NSString *cityID = [[NSUserDefaults standardUserDefaults]objectForKey:@"city_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSDictionary *parameters = @{@"title":self.OnedetailCell.titleText.text,
                                 @"gender":[NSString stringWithFormat:@"%i",self.OnedetailCell.segmengIndex],
                                 @"age":self.OnedetailCell.ageText.text,
                                 @"marital_status":self.OnedetailCell.MarriageText.text,
                                 @"census":self.OnedetailCell.addressText.text,
                                 @"district":self.OnedetailCell.strAddressID,
                                 @"district_name":self.OnedetailCell.NowAddressText.text,
                                 @"career":self.TwodetailCell.WorkTypeText.text,
                                 @"degree":self.TwodetailCell.educationType,
                                 @"salary":self.TwodetailCell.monthlySalaryType,
                                 @"car_house":self.TwodetailCell.haveCarType,
                                 @"require":self.TwodetailCell.RequirementText.text,
                                 @"qq":self.TwodetailCell.QQText.text,
                                 @"wechat":self.TwodetailCell.WeChatText.text,
                                 @"contact":self.OnedetailCell.ContactTetx.text,
                                 @"phone":self.OnedetailCell.ContactTelphoneText.text,
                                 @"description":self.setUpImageCell.fieldIntroduction.text,
                                 @"city":cityID,
                                 };
    [self.dictPhotoIDs addEntriesFromDictionary:parameters];

    [HttpRequestManager postMethod:KInterface_PublishFriend parameters:self.dictPhotoIDs completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            MakefriendsViewController *makeFriendVC = [[MakefriendsViewController alloc]init];
            [self.navigationController pushViewController:makeFriendVC animated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];

}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return [MakefirendsReleaseOneCell heightOfTableCell];
    }else if(indexPath .section == 1)
    {
        return [MakefirendsReleaseTwoCell heightOfTableCell];
    }else{
        return [SetUpImageCell heightOfTableCell];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F6F5F1"]];
    return sectionView;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        self.OnedetailCell = [tableView dequeueReusableCellWithIdentifier:MakefirendsReleaseOneCellID];
        self.OnedetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return self.OnedetailCell;
    }else if(indexPath.section == 1)
    {
        self.TwodetailCell = [tableView dequeueReusableCellWithIdentifier:MakefirendsReleaseTwoCellID];
        self.TwodetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return self.TwodetailCell;
    }else{
        self.setUpImageCell = [tableView dequeueReusableCellWithIdentifier:SetUpImageCellID];
        self.setUpImageCell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.setUpImageCell.delegate = self;
        return self.setUpImageCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.ReleaseTableView.height = CGRectGetHeight(AppBounds)-64-CGRectGetHeight(keyboardFrame);
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    self.ReleaseTableView.height = CGRectGetHeight(AppBounds)  - 64 ;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
